<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0}
        img{
            vertical-align: top;
        }
        .main{
            margin:0 auto;
            width:1000px;
            margin-top:10px;

        }
        .fixed {
            position: fixed;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
<div class="top" id="top">
    <img src="images/top.png" alt=""/>
</div>
<div id="Q-nav">
    <img src="images/nav.png" alt=""/>
</div>
<div class="main" id="main">
    <img src="images/main.png" alt=""/>
</div>
</body>
</html>
<script src="my.js" type="text/javascript"></script>
<script>

    var nav = document.getElementById("Q-nav");
    var val = document.getElementById("top").offsetHeight;  // 得到自身的高度
    var main = document.getElementById("main");
    // alert(val);
    window.onscroll = function() {
        console.log(document.body.scrollTop);
        if(scroll().top>=val) {
           //  alert("到了")
            nav.className = "fixed";
            main.style.paddingTop = "83px";
        }
        else {
            nav.className = "";
            main.style.paddingTop = "0";
        }
    }
</script>
